<?php
namespace app\api\controller;
use think\facade\Db;
class ProductType extends common
{
    private $dbname = 'product_type';


    /**
     * 列表分页获取
     * @return string
     */
    public function getlist()
    {

        $list = Db::name($this->dbname)->order('sort desc')->select()->toArray();

        return re_success('获取成功',$list);
    }
    /**
     * 列表分页获取
     * @return string
     */
    public function getSeleList()
    {
        $data = input('get.');
        $where = [];
        if(!empty($data['id']) || !empty($data['name'])){
            $list = Db::name($this->dbname)->whereOr('id','=',$data['id'])->whereOr('name','like','%'.$data['name'].'%')->field('id,name,pid,status')->select()->toArray();

        }else{
            $list = Db::name($this->dbname)->field('id,name,status,pid')->select()->toArray();

        }
        return re_success('获取成功',$list);
    }
    /**
     * 添加
     * @return false|string|void
     */
    public function insert()
    {

        $data = input('post.');
        $data['user_id'] = $this->getThisUser()['uid'];
        //模型数据验证
        $model = new \app\api\model\ProductType();
        //模型写入
        return $model->add($data);
    }

    /**
     * 修改
     * @return false|string|void
     */
    public function update()
    {
        $data = input('put.');
        //模型验证
        $data['user_id'] = $this->getThisUser()['uid'];
        $model = new \app\api\model\ProductType();
        return $model->edit($data);

    }

    /**查询采用视图查询
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        $info = Db::name($this->dbname)
            ->find(input('get.id'));
        if($info){

            return re_success('获取成功',$info);
        }else{
            return re_error('获取失败');
        }
    }


    /**
     * 删除
     * @return false|string|void
     */
    public function delete()
    {
        $data = input('delete.id');
        $model = new \app\api\model\ProductType();
        return $model->dele($data);
    }
}
